package com.ruoyi.StarStudio.Service.Impl;

import com.ruoyi.StarStudio.DAO.CouponsDAO;
import com.ruoyi.StarStudio.DAO.CouponsStockDAO;
import com.ruoyi.StarStudio.Entity.Coupons;
import com.ruoyi.StarStudio.Entity.CouponsChooseResult;
import com.ruoyi.StarStudio.Entity.CouponsStock;
import com.ruoyi.StarStudio.Service.CouponsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Timestamp;
import java.util.Date;
import java.util.List;

@Service
public class CouponsServiceImpl implements CouponsService {

    @Autowired
    CouponsDAO couponsDAO;

    @Autowired
    CouponsStockDAO couponsStockDAO;

    @Override
    public List<Coupons> findCouponsList(Coupons coupons) {
        return couponsDAO.findCouponsList(coupons);
    }

    @Override
    public void addCoupons(Coupons coupons) {
        //向优惠券库存明细中添加数据
        int Stock = coupons.getStock();
        String url = "http://yibinmall.chenglee.top:8080/exchange/check/";
        couponsDAO.addCoupons(coupons);
        if (Stock > 0){
            for (int i = 0; i < Stock; i++) {
                CouponsStock couponsStock = new CouponsStock();
                couponsStock.setCoupons_id(coupons.getCoupon_id());
                couponsStock.setHave_been_sold(false);
                couponsStock.setAdd_time(new Timestamp(new Date().getTime()));
                couponsStock.setCoupons_name(coupons.getCoupon_name());
                couponsStockDAO.addCouponsStock(couponsStock);
                couponsStock.setQr_code(url + couponsStock.getItem_id());
                couponsStockDAO.updateCouponsStock(couponsStock);
            }
        }
    }

    @Override
    public void DeleteCouponsById(int id) {
        couponsDAO.deleteCouponsById(id);
    }

   public void updateCoupons(Coupons coupons){
       int couponsNewStock = coupons.getStock();
       int couponsOldStock = couponsDAO.findStockByCouponsId(coupons.getCoupon_id());
       if (couponsNewStock > couponsOldStock){  /*如果增加库存*/
           //向优惠券库存个体表添加数据
           CouponsStock couponsStock = new CouponsStock();
           couponsStock.setCoupons_id(coupons.getCoupon_id());
           couponsStock.setHave_been_sold(false);
           couponsStock.setAdd_time(new Timestamp(new Date().getTime()));
           couponsStock.setCoupons_name(coupons.getCoupon_name());
           int addNum = couponsNewStock - couponsOldStock;
           for (int i = 0; i < addNum; i++) {
               couponsStockDAO.addCouponsStock(couponsStock);
           }
       }else if (couponsNewStock < couponsOldStock){ /*如果减少库存*/
           //删除多余优惠券库存
           int subNum = couponsOldStock - couponsNewStock;
           List<Long> couponsStockIds = couponsStockDAO.findCouponsStockIds(coupons.getCoupon_id());
           for (int i = 0; i < subNum; i++) {
               couponsStockDAO.deleteCouponsStock(Math.toIntExact(couponsStockIds.get(i)));
           }
       }
       couponsDAO.updateCoupons(coupons);
   }

    /**
     * 查询总记录数
     * @return 总记录数
     */
    @Override
    public long findAllRecordNumber() {
        return couponsDAO.findAllRecordNumber();
    }

    @Override
    public Coupons findByCoupons_item_id(long coupon_item_id) {
        return couponsDAO.findByCoupons_item_id(coupon_item_id);
    }

    @Override
    public Coupons findByCouponsId(long coupons_id) {
        return couponsDAO.findByCoupon_id(coupons_id);
    }

    @Override
    public List<CouponsChooseResult> getCouponsChooseResultList() {
        return couponsDAO.getCouponsChooseResultList();
    }
}
